//Defining function which loads Hitokoto in the background
var hitokotoData=new Object();
function loadHitokoto(){
    var arg=new Object();
    if (localStorage.uid)
    {arg.uid=localStorage.uid}
    if (localStorage.catstr)
    {arg.cat=localStorage.catstr}
    else
    {arg.cat="a,b,d"}
    $.getJSON('http://api.hitokoto.us/rand',arg,function(d){
	hitokotoData.content=d.hitokoto;
	hitokotoData.from="From"+d.catname+" "+d.source;
    });
}
//Loading Hitokoto on extension startup.
loadHitokoto();
//Setting message listener.
chrome.extension.onMessage.addListener(function(message,sender,response){
    //Answering Content Request.
    if (message.type=="contentRequest")
	{
	    response(hitokotoData);
	    //Refreshing cache.
	    loadHitokoto();
	}
    else
    {response({});}
});
chrome.tabs.onUpdated.addListener(function(id,changeInfo,tab){
    if (changeInfo.status=="complete")
    {	
	if ((!localStorage.bar)||(localStorage.bar=="1"))
	{
	    chrome.tabs.executeScript(id,{file:"js/jquery.js"},function(){
		chrome.tabs.executeScript(id,{file:"js/bar.js"});
	    });
	}
    }
});
